package com.mb.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbBannerInfo;
import com.mb.app.domain.TbProductInfo;
import com.mb.app.domain.TbProductInfo;
import com.mb.app.dao.TbProductInfoMapper;
import com.mb.app.service.ITbProductInfoService;
import com.mb.common.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品信息  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-03-21
 */
@Service
@Slf4j
public class TbProductInfoServiceImpl extends ServiceImpl<TbProductInfoMapper, TbProductInfo> implements ITbProductInfoService {

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public List<TbProductInfo> queryList(Map<String, Object> map) {
        List<TbProductInfo> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int save(TbProductInfo TbProductInfo) {
        TbProductInfo.setCreateTime(new Date());
        if (StringUtils.isNotEmpty(TbProductInfo.getLogoImg())) {
            String logoImg = TbProductInfo.getLogoImg();
            logoImg = logoImg.replaceAll(commonConfig.getImgDomain(), "");
            TbProductInfo.setLogoImg(logoImg);
        }
        return this.baseMapper.insert(TbProductInfo);

    }

    @Override
    public int copy(TbProductInfo TbProductInfo) {
        // 查询该分类下面有没有该产品名称
        Map<String, Object> map = new HashMap<>();
        map.put("classId", TbProductInfo.getClassId());
        map.put("productName", TbProductInfo.getProductName());
        List<TbProductInfo> tbProductInfos = this.baseMapper.selectByProductInfo(map);
        if (tbProductInfos != null && tbProductInfos.size() > 0) {
            return 0;
        } else {
            TbProductInfo.setCreateTime(new Date());
            if (StringUtils.isNotEmpty(TbProductInfo.getLogoImg())) {
                String logoImg = TbProductInfo.getLogoImg();
                logoImg = logoImg.replaceAll(commonConfig.getImgDomain(), "");
                TbProductInfo.setLogoImg(logoImg);
            }
            return this.baseMapper.insert(TbProductInfo);
        }
    }

    @Override
    public int update(TbProductInfo TbProductInfo) {
        try {
            TbProductInfo TbProductInfo1 = new TbProductInfo();
            TbProductInfo1.setProductId(TbProductInfo.getProductId());
            TbProductInfo1.setDataStatus(1);
            TbProductInfo TbProductInfoDO = this.baseMapper.selectOne(TbProductInfo1);
            if (TbProductInfoDO != null) {
                if (StringUtils.isNotEmpty(TbProductInfo.getLogoImg())) {
                    String logoImg = TbProductInfo.getLogoImg();
                    logoImg = logoImg.replaceAll(commonConfig.getImgDomain(), "");
                    TbProductInfo.setLogoImg(logoImg);
                }
                TbProductInfo.setUpdateTime(new Date());
                this.baseMapper.updateById(TbProductInfo);
            }
        } catch (Exception e) {
            log.error("update Exception", e);
        }
        return 0;
    }


    @Override
    public int remove(Long productId, Long delUserId) {
//        return this.baseMapper.deleteById(appId);
        TbProductInfo TbProductInfo = new TbProductInfo();
        TbProductInfo.setProductId(productId);
        TbProductInfo.setDelTime(new Date());
        TbProductInfo.setDataStatus(0);
        TbProductInfo.setDelUserId(delUserId);
        return this.baseMapper.updateById(TbProductInfo);
    }

    @Override
    public TbProductInfo getDtl(Long productId) {
        TbProductInfo TbProductInfo = this.baseMapper.selectById(productId);
        if (TbProductInfo != null && StringUtils.isNotEmpty(TbProductInfo.getLogoImg())) {
            TbProductInfo.setLogoImg(commonConfig.getImgDomain() + TbProductInfo.getLogoImg());
        }
        return TbProductInfo;
    }

    @Override
    public boolean productInfoNameExit(Map<String, Object> params) {
        boolean exit;
        exit = this.baseMapper.selectByProductInfo(params).size() > 0;
        return exit;
    }

    @Override
    public int status(Long productId, Integer status) {
//        return this.baseMapper.deleteById(appId);
        TbProductInfo tbProductInfo = new TbProductInfo();
        tbProductInfo.setProductId(productId);
        tbProductInfo.setStatus(status);
        return this.baseMapper.updateById(tbProductInfo);
    }
}
